package com.spring.cloud.study.controller;

import com.spring.cloud.study.entity.SysMenu;
import com.spring.cloud.study.service.SysMenuService;
import com.spring.cloud.study.utils.data.PageUtils;
import com.spring.cloud.study.utils.data.R;
import com.spring.cloud.study.utils.security.PrincipalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: 齐丹枫
 * Date: 2018/3/7
 * Time: 13:29
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@RestController
@RequestMapping("/api/sys/sysMenu")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 生成菜单列表
     * @param userName
     * @return
     */
    @RequestMapping("/getMenuList")
    public R getUserMenu(@RequestParam("userName") String userName){
        List<SysMenu> userMenusPermission = sysMenuService.getUserMenuPremission(userName);
        Set<String> userOptionsPermission =  PrincipalUtils.getUserOptionPermission();
        return R.ok().put("userMenusPermission",userMenusPermission)
                .put("userOptionsPermission",userOptionsPermission);
    }

    /**
     *
     * @param menuId
     * @return
     */
    @RequestMapping("/getMenuTreeByParentId")
    public R getMenuTreeByParentId(@RequestParam(value = "id") int menuId){
        List<SysMenu> sysMenus = sysMenuService.findByParentId(menuId);
        return R.ok().put("data",sysMenus);
    }

    @RequestMapping("/getMenuIsDirectories")
    public R getMenuIsDirectories(){
        List<SysMenu> sysMenus = sysMenuService.findByTypeIs(0);
        return R.ok().put("data",sysMenus);
    }


    /**
     * 由用户名，得到所有权限树
     * @param userName
     * @return
     */
    @RequestMapping("/getUserAllPremissionTreeList")
    public R getUserAllPremissionTreeList(@RequestParam  String userName){
        List<SysMenu> all = sysMenuService.getUserAllPremissionTreeList(userName);
        return R.ok().put("data",all);
    }

    @RequestMapping("/findMenu")
    public R find(@RequestParam Map<String,Object> params){
        Page<SysMenu> page = sysMenuService.findPage(params);
        List<SysMenu> menus = page.getContent();
      /*  List<SysMenu> collect1 = menus.parallelStream()
                .filter(sysMenu -> sysMenu.getType().equals("0")).collect(Collectors.toList());
        if(collect1.size() == 0){
            collect1 = menus.parallelStream()
                    .filter(sysMenu -> sysMenu.getType().equals("1")).collect(Collectors.toList());
        }
        if(collect1.size() != 0){
            Map<Integer, List<SysMenu>> collect = menus.parallelStream()
                    .collect(Collectors.groupingBy(SysMenu::getParentId));
            menus = sysMenuService.setMenuTreeList(collect1,collect);
        }*/


        PageUtils pageUtils = new PageUtils(sysMenuService.generateMenuTree(menus),page.getTotalElements(),page.getNumberOfElements(),Integer.parseInt(params.get("page").toString()));
        return R.ok().put("data",pageUtils);
    }


    @RequestMapping("/getMenuTreeByRoleId")
    public R getMenuTreeByRoleId(@RequestParam("id") int roleId){
        List<SysMenu> sysMenus = sysMenuService.getMenuTreeByRoleId(roleId);
        return R.ok().put("data",sysMenus);
    }


    @RequestMapping("/save")
    public R save(@RequestBody SysMenu sysMenu){
        sysMenuService.save(sysMenu);
        return R.ok();
    }

    @RequestMapping("/update")
    public R update(@RequestBody SysMenu sysMenu){
        sysMenuService.update(sysMenu);
        return R.ok();
    }

    @RequestMapping("/deleteById")
    public R deleteById(@RequestParam int id){
        try {
            sysMenuService.delete(id);
        }catch (Exception e){
            R.error();
        }
        return R.ok();
    }

    @RequestMapping("/deleteBatchById")
    public R deleteBatchById(@RequestBody List<Integer> ids){
        try {
            sysMenuService.deleteBatch(ids);
        }catch (Exception e){
           return R.error();
        }

        return R.ok();
    }

}
